home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7741 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: nwlink.com!usenet
  2. From: Teresa Reiko <tjr19@mail.nwlink.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Permutations
  5. Date: 28 Feb 1996 16:15:03 GMT
  6. Organization: Northwest Link
  7. Message-ID: <4h1v27$cdc@texas.nwlink.com>
  8. References: <Dn8yz9.GGy@spuddy.mew.co.uk>
  9. NNTP-Posting-Host: port31.annex3.nwlink.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.2 (Windows; U; 16bit)
  14.  
  15. david@spuddy.mew.co.uk (David Turner) wrote:
  16.  
  17. > Does anyone know of an algorithm that works out the different permutations
  18. >of ordering a number of objects? Not calculating how many there are, but 
  19. >actaully working out (quickly?) what they permutations are!
  20.  
  21. Untested code -- but this should work...
  22. Example:  Permute("", "abcdef", 6)
  23.  
  24. void Permute(char *p_prefix, char *p_list, int num)
  25. {
  26.         int i;
  27.     char s[10], p[10];
  28.  
  29.     if(num == 0)
  30.     {
  31.         printf("%s%s", prefix, p_list);
  32.         return;
  33.     }
  34.  
  35.     for(i = 0; i < num; i++)
  36.     {
  37.         s[1] = 0;
  38.         s[0] = p_list[i];
  39.         strcpy(p, p_prefix);
  40.         strcat(p, s);
  41.         
  42.         strncpy(s, p_list, i);
  43.         s[i] = 0;
  44.         strcat(s, p_list + i + 1);
  45.  
  46.         Permute(p, s, num - 1);
  47.     }
  48. }       
  49.  
  50. ----------------------------------------------------------------------
  51. Teresa Reiko    Chief Programmer, Tenbyte Software    tjr19@nwlink.com
  52. ----------------------------------------------------------------------
  53.  
  54.  
  55.